package com.cskaoyan.service.vx.comment;


import com.cskaoyan.bean.bo.vxBo.comment.CommentPostBo;
import com.cskaoyan.bean.po.wxPo.MarketComment;
import com.cskaoyan.bean.po.wxPo.MarketCommentExample;
import com.cskaoyan.bean.po.wxPo.MarketUser;
import com.cskaoyan.bean.po.wxPo.MarketUserExample;
import com.cskaoyan.bean.vo.vxVo.UserInfo;
import com.cskaoyan.bean.vo.vxVo.comment.CommentCountVo;
import com.cskaoyan.bean.vo.vxVo.comment.CommentListVo;
import com.cskaoyan.bean.vo.vxVo.comment.CommentPostVo;
import com.cskaoyan.mapper.wxMapper.MarketCommentMapper;

import com.cskaoyan.mapper.wxMapper.MarketUserMapper;
import com.cskaoyan.util.StringToArrayUtils;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *  该实现类负责核心逻辑  wx/comment/list/
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/7 21:00
 */
@Service("vxcommentserviceimpl")
public class CommentServiceImpl implements CommentService{
    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public List<CommentListVo> list(Integer valueId, Integer type, Integer limit, Integer page, Integer showType) {
        PageHelper.startPage(page, limit); // 开启分页
        /**
         * 查询 market_comment表
         */
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        // 向criteria里添加条件;
        criteria.andValueIdEqualTo(valueId);
        /**
         * 修改了 关于 type 的 getter/setter / Criteria andTypeEqualTo(Integer value); 等相关;
         */
        criteria.andTypeEqualTo(type);
        // todo: showType 先不考虑;
        // 从market_comment查出来 ; PO对象; 此时里面"需要连表查询的哪些部分" 还是 null 的;
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        //
        List<CommentListVo> commentListVos = new ArrayList<CommentListVo>();

        /**
         * 查询 market_user 表; 当然是需要从 market_comment表 查出 userId;
         */
        // 从marketComments可以获取到 vo 需要的user_Id  等其他因素;

        for (int i = 0; i < marketComments.size(); i++) {
            MarketComment marketComment = marketComments.get(i);
            Integer userId =marketComment.getUserId();
            // 查询 当前 userId 对应信息;
            // MarketUserExample marketUserExample = new MarketUserExample();
            // MarketUserExample.Criteria criteria1 = marketUserExample.createCriteria();
            // criteria1.andIdEqualTo(userId);
            // marketUsers 应该仅有一个, 虽然是一个 list;
            // List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
            /**
             * 根据 userId 来 select 一个用户;
             */
            MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
            String avatarUrl = marketUser.getAvatar();
            String nickname = marketUser.getNickname();
            // 注意 NPE 异常;
            CommentListVo commentListVo = new CommentListVo();
            // 使用构造器;
            commentListVo.setUserInfo(new UserInfo(avatarUrl,nickname));

            //其他参数;
            commentListVo.setAddTime(marketComment.getAddTime());
            // 获取 picUrls 是 "["", "" ,"" ]" 形式; 而 需要转化;
            String picUrls = marketComment.getPicUrls();
            System.out.println("picUrls = " + picUrls);
            //  commentListVo.setPicList(marketComment.getPicUrls());
             commentListVo.setAdminContent(marketComment.getAdminContent());
             commentListVo.setContent(marketComment.getContent());
            // commentListVos
            commentListVos.add(commentListVo);
        }



        return commentListVos;

    }

    /**
     * 由 mapper 根据参数条件获取到多个符合的 mareket_comment  record, 并映射到Po 对象里;
     * @param valueId
     * @param type
     * @return
     */
    @Override
    public CommentCountVo count(Integer valueId, Integer type) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        marketCommentExample.createCriteria().andValueIdEqualTo(valueId).andTypeEqualTo(type);
        /**
         * 返回一个 list;
         * 有可能返回多个;
         */
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        Integer allCount = marketComments.size();
        // i 是 有图 的统计个数;
        int i = 0 ;
        for (MarketComment marketComment : marketComments) {
            if (marketComment.getHasPicture()) {
                i++;
            }
        }
        // 由 PO->VO 对象;
        CommentCountVo commentCountVo = new CommentCountVo();
        commentCountVo.setAllCount(allCount);
        commentCountVo.setHasPicCount(i);
        return  commentCountVo;
    }

    /**
     * 客户端参数:
     *  {
     *     "type":1,
     *     "valueId":"368",
     *     "content":"弟弟",
     *     "star":5,
     *     "hasPicture":true,
     *     "picUrls":[
     *         "http://182.92.235.201:8083/wx/storage/fetch/mc7kwj6ails3yk02an4z.png"
     *     ]
     * }
     * @param commentPostBo
     * @return
     */
    @Override
    public CommentPostVo post(CommentPostBo commentPostBo) {
        MarketComment marketComment = new MarketComment();
        marketComment.setContent(commentPostBo.getContent());
        marketComment.setHasPicture(commentPostBo.getHasPicture());
        marketComment.setPicUrls(commentPostBo.getPicUrls().toString());
        marketComment.setStar(commentPostBo.getStar());
        marketComment.setType(commentPostBo.getType());
        marketComment.setValueId(commentPostBo.getValueId());
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        marketComment.setUserId(user.getId());// 获取当前用户的 id;
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();

        // 插入一条 record;
        int count = marketCommentMapper.insertReturnId(marketComment);
        // 可以获取到 id;
        //  PO->VO
        CommentPostVo commentPostVo = new CommentPostVo();
        commentPostVo.setId(marketComment.getId());
        commentPostVo.setValueId(marketComment.getValueId());
        commentPostVo.setType(marketComment.getType());
        commentPostVo.setContent(marketComment.getContent());
        commentPostVo.setUserId(marketComment.getUserId());
        commentPostVo.setHasPicture(marketComment.getHasPicture());
        // List<Integer> integers = StringToArrayUtils.toArray(marketComment.getPicUrls());
        commentPostVo.setPicUrls(commentPostBo.getPicUrls()); // 可以直接从 VXCollectAddOrDeleteBo 对象获取;  BO->PO: 将
        commentPostVo.setStar(marketComment.getStar());
        commentPostVo.setAddTime(marketComment.getAddTime());
        commentPostVo.setUpdateTime(marketComment.getUpdateTime());

        return commentPostVo;
    }
}
